# -*- coding: utf-8 -*-
from django.contrib import admin
from atendimento_cliente.models import *

class ViewAdmin(admin.ModelAdmin):

    """
    Custom made change_form template just for viewing purposes
    You need to copy this from /django/contrib/admin/templates/admin/change_form.html
    And then put that in your template folder that is specified in the 
    settings.TEMPLATE_DIR
    """
    #change_form_template = 'view_form.html'

    # Remove the delete Admin Action for this Model
    actions = None

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    def save_model(self, request, obj, form, change):
        #Return nothing to make sure user can't update any data
        pass


class TipoChamadoAdmin(admin.ModelAdmin):
    
    fields = ['nome']
    list_display = ['id','nome']
    list_display_links = ['id','nome']
    search_fields = ['nome']

class PerguntaAdmin(ViewAdmin):
    
    fields = ['data', 'descricao']
    list_display = ['id', 'data', 'descricao']
    list_display_links = ['id','descricao']
    search_fields = ['data', 'descricao']
    date_hierarchy = 'data'
    
    #def has_add_permission(self, request):
       # return False
    
class RespostaAdmin(admin.ModelAdmin):
    
    fields = ['data', 'descricao']
    list_display = ['id', 'data', 'descricao']
    list_display_links = ['id','descricao']
    search_fields = ['data', 'descricao']
    date_hierarchy = 'data'
        
    def has_add_permission(self, request):
        return False

class RespostaInline(admin.StackedInline):
    model = Resposta
    extra = 0

class PerguntaInline(admin.StackedInline):
    model = Pergunta
    extra = 0

class ChamadoAdmin(admin.ModelAdmin):
    
    fieldsets = (
        (u'Informações', {
            'classes': ['wide', 'extrapretty'],
            'fields': ['cliente', 'grupo','tipo', 'status']
        }),
        ('Datas', {
            'classes': ['wide', 'extrapretty'],
            'fields': ['abertura', 'atualizacao', 'fechamento']
        }),
    )
    
    list_display = ['id', 'cliente', 'grupo', 'tipo', 'status','abertura', 'fechamento']
    list_display_links = ['id', 'cliente', 'grupo', 'tipo', 'status']
    list_filter = ['tipo','status']
    search_fields = ['cliente', 'grupo']
    date_hierarchy = 'abertura'

    inlines = [PerguntaInline, RespostaInline]
    
    def queryset(self,request):
        username = str(request.user.first_name) + ' ' + str(request.user.last_name)
        query = super(ChamadoAdmin, self).queryset(request)
        groups = request.user.groups.all()
        for i in groups:
            if i.name == 'Cliente':
                return query.filter(cliente = username)
        return query
        

admin.site.register(TipoChamado, TipoChamadoAdmin)
admin.site.register(Chamado, ChamadoAdmin)
admin.site.register(Pergunta, PerguntaAdmin)
admin.site.register(Resposta, RespostaAdmin)
